package com.pan.admin.module.channel.controller;

import com.gitee.apanlh.annotation.viewresolver.RequestParamVo;
import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.pan.admin.cache.DistrictCache;
import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.module.channel.entity.LoanChannelFlowConfig;
import com.pan.admin.module.channel.entity.LoanChannelFlowConfigIPCityPO;
import com.pan.admin.module.channel.entity.LoanChannelFlowConfigIpOffModule;
import com.pan.admin.module.channel.entity.vo.LoanChannelFlowConfigIPCityVO;
import com.pan.admin.module.channel.service.LoanChannelFlowConfigService;
import com.pan.admin.module.common.entity.DistrictPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 渠道流程控制配置(动画、是否开启验证码、开启协议、隐私协议等等))Controller
 *
 * @author ts
 * @date 2023-09-27
 */
@RestController
@RequestMapping("/channel/config")
public class LoanChannelFlowConfigController extends BaseController {
    @Autowired
    private LoanChannelFlowConfigService loanChannelFlowConfigService;

    /**
     * 查询渠道流程控制配置(动画、是否开启验证码、开启协议、隐私协议等等))列表
     */
    @PreAuthorize("@ss.hasPermi('channel:config')")
    @GetMapping("/list")
    public TableDataInfo list(LoanChannelFlowConfig loanChannelFlowConfig) {
        startPage();
        List<LoanChannelFlowConfig> list = loanChannelFlowConfigService.list(loanChannelFlowConfig);
        return getDataTable(list);
    }

    /**
     * 获取渠道流程控制配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('channel:config')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(loanChannelFlowConfigService.getById(id));
    }

    /**
     * 新增渠道流程控制配置(动画、是否开启验证码、开启协议、隐私协议等等))
     */
    @PreAuthorize("@ss.hasPermi('channel:config')")
    @Log(title = "渠道流程控制配置(动画-验证码-协议等)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanChannelFlowConfig loanChannelFlowConfig) {
        return toAjax(loanChannelFlowConfigService.insertLoanChannelFlowConfig(loanChannelFlowConfig));
    }

    /**
     * 修改渠道流程控制配置(动画、是否开启验证码、开启协议、隐私协议等等))
     */
    @PreAuthorize("@ss.hasPermi('channel:config')")
    @Log(title = "渠道流程控制配置(动画-验证码-协议等)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanChannelFlowConfig loanChannelFlowConfig) {
        return toAjax(loanChannelFlowConfigService.updateLoanChannelFlowConfig(loanChannelFlowConfig));
    }

    /**
     *  获取IP城市留资列表
     */
    @PreAuthorize("@ss.hasPermi('channel:config')")
    @Log(title = "渠道流程控制配置(动画-验证码-协议等)", businessType = BusinessType.UPDATE)
    @GetMapping("/listIpCity")
    public AjaxResult listIpCity(Long loanChannelId) {
        if (loanChannelId == null) {
            return AjaxResult.error("非法操作");
        }

        List<LoanChannelFlowConfigIPCityPO> list = loanChannelFlowConfigService.listIpCity(loanChannelId);

        //  合并编码-[父编码,子编码]
        List<String> margeList = CollUtils.newArrayList();
        //  根据子集编码搜索父类编码合成
        IteratorUtils.array(list, t -> {
            String areaCode = t.getAreaCode();
            DistrictPO districtPO = DistrictCache.getByCode(areaCode);
            String pCode = districtPO.getPCode();
            //  改变原有pCode值,县级市合并为市级
            margeList.add(pCode.substring(0, 2) + "0000" + "," + areaCode);
        });
        return success(margeList);
    }

    /**
     *  获取IP城市留资列表
     *
     *  @param  requestVO 地区编码
     *  @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('channel:config')")
    @Log(title = "渠道流程控制配置(动画-验证码-协议等)", businessType = BusinessType.UPDATE)
    @PostMapping("/addIpCity")
    public AjaxResult addIpCity(@RequestParamVo RequestVO requestVO) {
        LoanChannelFlowConfigIPCityVO loanChannelFlowConfigIPCityVO = requestVO.toBean(LoanChannelFlowConfigIPCityVO.class);
        Long loanChannelId = loanChannelFlowConfigIPCityVO.getLoanChannelId();
        String areaCodes = loanChannelFlowConfigIPCityVO.getAreaCodes();

        if (loanChannelId == null) {
            return AjaxResult.error("非法操作");
        }

        loanChannelFlowConfigService.batchInsertIpCity(loanChannelId, areaCodes);
        return toAjax(1);
    }

    /**
     *  获取IP城市屏蔽列表
     */
    @PreAuthorize("@ss.hasPermi('channel:config')")
    @Log(title = "渠道流程控制配置(获取IP城市屏蔽列表)", businessType = BusinessType.UPDATE)
    @GetMapping("/listIpOffModule")
    public AjaxResult listIpOffModule(Long loanChannelId) {
        if (loanChannelId == null) {
            return AjaxResult.error("非法操作");
        }

        List<LoanChannelFlowConfigIpOffModule> list = loanChannelFlowConfigService.listIpOffModule(loanChannelId);

        //  合并编码-[父编码,子编码]
        List<String> margeList = CollUtils.newArrayList();
        //  根据子集编码搜索父类编码合成
        IteratorUtils.array(list, t -> {
            String areaCode = t.getAreaCode();
            DistrictPO districtPO = DistrictCache.getByCode(areaCode);
            String pCode = districtPO.getPCode();
            //  改变原有pCode值,县级市合并为市级
            margeList.add(pCode.substring(0, 2) + "0000" + "," + areaCode);
        });
        return success(margeList);
    }

    /**
     *  添加IP城市屏蔽列表
     *
     *  @param  requestVO 地区编码
     *  @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('channel:config')")
    @Log(title = "渠道流程控制配置(添加IP城市屏蔽列表)", businessType = BusinessType.UPDATE)
    @PostMapping("/addIpOffModule")
    public AjaxResult addIpOffModule(@RequestParamVo RequestVO requestVO) {
        LoanChannelFlowConfigIPCityVO loanChannelFlowConfigIPCityVO = requestVO.toBean(LoanChannelFlowConfigIPCityVO.class);
        Long loanChannelId = loanChannelFlowConfigIPCityVO.getLoanChannelId();
        String areaCodes = loanChannelFlowConfigIPCityVO.getAreaCodes();

        if (loanChannelId == null) {
            return AjaxResult.error("非法操作");
        }

        loanChannelFlowConfigService.batchInsertIpOffModule(loanChannelId, areaCodes);
        return toAjax(1);
    }
}
